package cc.catface.service;

import cc.catface.common.output.Result;
import cc.catface.dao.biz.app.ApUser;
import cc.catface.dao.biz.app.ApUserChannel;
import cc.catface.dao.biz.app.dto.*;
import cc.catface.dao.biz.app.mapper.ApUserChannelMapperServiceImpl;
import cc.catface.dao.biz.app.mapper.ApUserMapperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApUserServiceImpl implements ApUserService {
    @Autowired
    private ApUserMapperServiceImpl apUserMapperService;
    @Autowired
    private ApUserChannelMapperServiceImpl apUserChannelMapperService;


    @Override
    public Result<ApUser> info(InfoINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户信息");
        return Result.success(user);
    }

    @Override
    public Result<UpdateInfoOUTDTO> updateInfo(UpdateInfoINDTO in) {
        UpdateWrapper<ApUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("phone", in.getPhone());
        updateWrapper.set("name", in.getName());
        updateWrapper.set("password", in.getPassword());
        updateWrapper.set("image", in.getImage());
        updateWrapper.set("sex", in.getSex());
        boolean update = apUserMapperService.update(updateWrapper);
        UpdateInfoOUTDTO out = new UpdateInfoOUTDTO();
        out.setSuccess(update);
        return Result.success(out);
    }

    @Override
    public Result<LoginOUTDTO> login(LoginINDTO in) {
        String phone = in.getPhone();
        QueryWrapper<ApUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("password", in.getPassword());
        List<ApUser> list = apUserMapperService.list(queryWrapper);
        if (list == null || list.size() < 1) return Result.error("帐号或密码错误");

        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", phone));
        if (user == null) return Result.error("未查询到用户信息");
        LoginOUTDTO out = new LoginOUTDTO();
        out.setPhone(user.getPhone());
        out.setUserId(user.getId());
        out.setSuccess(true);
        return Result.success(out);
    }

    @Override
    public Result<List<GetUserChannelOUTDTO>> getUserChannel(GetUserChannelINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        List<ApUserChannel> channels = apUserChannelMapperService.list(new QueryWrapper<ApUserChannel>().eq("user_id", user.getId()));
        channels.sort((o1, o2) -> o1.getOrd() - o2.getOrd());
        List<GetUserChannelOUTDTO> outs = new ArrayList<>();
        for (ApUserChannel c : channels) {
            GetUserChannelOUTDTO out = new GetUserChannelOUTDTO();
            out.setChannelId(c.getChannelId());
            out.setChannelName(c.getName());
            out.setOrd(c.getOrd());
            out.setCreatedTime(c.getCreatedTime());
            outs.add(out);
        }
        return Result.success(outs);
    }

    @Override
    public Result<SaveUserChannelOUTDTO> saveUserChannel(SaveUserChannelINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        Long userId = user.getId();
        //先删了当前用户保存的频道列表
        apUserChannelMapperService.remove(new QueryWrapper<ApUserChannel>().eq("user_id", userId));
        for (SaveUserChannelINDTO.ChannelInfo c : in.getChannelInfos()) {
            ApUserChannel userChannel = new ApUserChannel();
            userChannel.setUserId(userId);
            userChannel.setChannelId(c.getChannelId());
            userChannel.setName(c.getChannelName());
            userChannel.setOrd(c.getOrd());
            userChannel.setCreatedTime(in.getCreatedTime());
            apUserChannelMapperService.save(userChannel);
        }
        SaveUserChannelOUTDTO out = new SaveUserChannelOUTDTO();
        out.setSuccess(true);
        return Result.success(out);
    }

    @Override
    public Result<RegistOUTDTO> regist(RegistINDTO in) {
        String phone = in.getPhone();
        if (phone == null || phone.trim().length() != 11) return Result.error("请输入正确的手机号");
        QueryWrapper<ApUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        List<ApUser> list = apUserMapperService.list(queryWrapper);
        if (list != null && list.size() > 0) return Result.error("帐号已存在");

        ApUser apUser = new ApUser();
        apUser.setName("");
        apUser.setPassword(in.getPassword());
        apUser.setPhone(phone);
        apUser.setImage("");
        apUser.setSex(2);
        apUser.setIsCertification(0);
        apUser.setIsIdentityAuthentication(0);
        apUser.setStatus(0);//正常
        apUser.setFlag(0);//普通用户
        apUser.setCreatedTime(in.getCreatedTime());
        apUserMapperService.save(apUser);
        RegistOUTDTO out = new RegistOUTDTO();
        out.setSuccess(true);
        out.setCreatedTime(in.getCreatedTime());
        return Result.success(out);
    }
}
